package com.koicarp.university.graduate.service.service.schoolInfoManage.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.koicarp.university.graduate.service.dao.ClassManageDao;
import com.koicarp.university.graduate.service.dto.schoolInfoManage.ClassManageDto;
import com.koicarp.university.graduate.service.entity.schoolInfoManage.ClassManage;
import com.koicarp.university.graduate.service.service.schoolInfoManage.IClassManageService;
import com.koicarp.university.graduate.service.vo.schoolInfoManage.ClassManageVo;
import com.koicarp.university.graudate.common.constant.BaseConstant;
import com.koicarp.university.graudate.common.utils.UniversityResponse;
import com.koicarp.university.graudate.common.utils.UniversityUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 【学校基本信息：班级管理表】(ClassManage)表服务实现类
 *
 * @author liutao
 * @since 2020-09-03 19:36:41
 */
@Service("classManageService")
@Slf4j
@RequiredArgsConstructor
public class ClassManageServiceImpl extends ServiceImpl<ClassManageDao,ClassManage> implements IClassManageService {

    private final ClassManageDao classManageDao;

    @Override
    public UniversityResponse getAllClassManage(ClassManage classManage) {
        UniversityResponse response=new UniversityResponse();
        response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
        response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);
        if (UniversityUtil.isNotNull(classManage.getSpecialityId())){
            classManage.setSpecialityId(classManage.getSpecialityId());
        }else {
            response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
            response.message(BaseConstant.RESPONSE_MESSAGE_OPR_FAILED);
            return response;
        }
        List<ClassManage> list = this.classManageDao.queryAll(classManage);
//        Map<String, Object> dataTable = UniversityUtil.getDataTable(list);
        response.data(list);
        return response;
    }

    @Override
    public UniversityResponse getClassManageInfoWithPage(ClassManageVo vo) {
        UniversityResponse response = new UniversityResponse();
        Page<ClassManage> page = null;
        boolean findOne = UniversityUtil.isNotNull(vo.getId());
        boolean isPage = UniversityUtil.isNotNull(vo.getPageNum())
                && UniversityUtil.isNotNull(vo.getPageSize());
        ClassManage classManage=new ClassManage();
        BeanUtils.copyProperties(vo,classManage);
        if (!findOne && !isPage){
            response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
            response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);
            Map listRes = new HashMap();
            listRes.put(BaseConstant.RESPONSE_RESULTSET,this.classManageDao.queryAll(classManage));
            response.data(listRes);
            return response;
        }
        if (findOne){
            page = new Page<>(1,1,false);
        } else{
            page = new Page<>(vo.getPageNum(),vo.getPageSize(),true);
        }
        IPage<ClassManageDto> result = null;
        if(UniversityUtil.isNotNull(vo.getSpecialityName())||UniversityUtil.isNotNull(vo.getAcademyName())){
            result = this.classManageDao.queryBlurClassInfoWithPage(page,vo); //模糊查
        }else {
            result = this.classManageDao.queryClassInfoWithPage(page, vo);// 标准查
        }
        if (findOne){
            List<ClassManageDto> list = result.getRecords();
            if (!UniversityUtil.isNotNull(list)){
                response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
                response.message(BaseConstant.RESPONSE_MESSAGE_OPR_RESULT_NULL+"!!!");
            } else {
                response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
                response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);
                response.data(list.get(0));
            }
        } else {
            response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
            response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);
            Map<String, Object> dataTable = UniversityUtil.getDataTable(result);
            response.data(dataTable);
        }
        return response;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ClassManage queryById(Integer id) {
        return this.classManageDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<ClassManage> queryAllByLimit(int offset, int limit) {
        return this.classManageDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param classManage 实例对象
     * @return 实例对象
     */
    @Override
    public UniversityResponse insert(ClassManage classManage) {
        UniversityResponse response = new UniversityResponse();
        response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
        response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);
//        LambdaQueryWrapper<Speciality> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(Speciality::getSpecialityCode,speciality.getSpecialityCode());
//        List<Speciality> list = specialityDao.selectList(wrapper);
//        if (!list.isEmpty()){
//            response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
//            response.message(speciality.getSpecialityCode()+BaseConstant.RESPONSE_MESSAGE_OPR_RESULT_DUPLICATION);
//            return response;
//        }
        int i = classManageDao.insert(classManage);
        if (i < 0){
            response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
            response.message(BaseConstant.RESPONSE_MESSAGE_OPR_FAILED);
            return response;
        }
        response.data(i);
        return response;
    }

    /**
     * 修改数据
     *
     * @param classManage 实例对象
     * @return 实例对象
     */
    @Override
    public UniversityResponse update(ClassManage classManage) {
        UniversityResponse response=new UniversityResponse();
        response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
        response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);
//        LambdaQueryWrapper<Speciality> wrapper=new LambdaQueryWrapper<>();
//        wrapper.eq(Speciality::getSpecialityCode,speciality.getSpecialityCode());
//        List<Speciality> academies = specialityDao.selectList(wrapper);
//        if (!academies.isEmpty()){
//            response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
//            response.message("专业代码"+BaseConstant.RESPONSE_MESSAGE_OPR_RESULT_DUPLICATION);
//            return response;
//        }
        int i = this.classManageDao.update(classManage);
        if (i < 0){
            response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
            response.message(BaseConstant.RESPONSE_MESSAGE_OPR_FAILED);
        }
        return response;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public UniversityResponse deleteById(Integer id) {
        int i = this.classManageDao.deleteById(id);
        UniversityResponse response=new UniversityResponse();
        response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
        response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);
        if (i > 0){
            return response;
        }
        response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
        response.message(BaseConstant.RESPONSE_MESSAGE_OPR_FAILED);
        return response;
    }

    @Override
    public UniversityResponse deleteListById(List<Integer> list) {
        int i =this.classManageDao.deleteListById(list);
        UniversityResponse response =new UniversityResponse();
        response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
        response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);
        if (i<0){
            response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
            response.message(BaseConstant.RESPONSE_MESSAGE_OPR_FAILED);
        }
        return response;
    }
}